﻿
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using 纸片人.InterfaceFolder;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace 纸片人.CalculateControls
{
    public class AddControl : OperatorControl
    {






        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
          
        }

        protected override void Timer_Tick(object sender, object e)
        {

            GetOneExportValue();
            GetTwoExportValue();
         
            if (oneConnectElement != null && twoConnectElement == null)
            {
                if (oneExportValue is double)
                {
                    SetType(1);
                }
                else if (oneExportValue is Vector2)
                {
                    SetType(2);
                }
                else if (oneExportValue is Vector3)
                {
                    SetType(3);
                }
                else if (oneExportValue is Vector4)
                {
                    SetType(4);
                }
                else
                {
                    SetType(1);
                    return;
                }
            }

            if(oneConnectElement==null && twoConnectElement != null)
            {
                if (twoExportValue is double)
                {
                    SetType(1);
                }
                else if (twoExportValue is Vector2)
                {
                    SetType(2);
                }
                else if (twoExportValue is Vector3)
                {
                    SetType(3);
                }
                else if (twoExportValue is Vector4)
                {
                    SetType(4);
                }
                else
                {
                    SetType(1);
                    return;
                }
            }


            try
            {

                if (oneExportValue is double)
                {
                    if (twoExportValue is double)
                    {
                        Value = (double)oneExportValue+ (double)twoExportValue;
                        SetType(2);
                    }
                    else if (twoExportValue is Vector2) {
                        Value = Vector2.Add(new Vector2((float)oneExportValue), (Vector2)twoExportValue);
                        SetType(2);

                    }
                    else if (twoExportValue is Vector3)
                    {
                     
                        Value = Vector3.Add(new Vector3((float)oneExportValue), (Vector3)twoExportValue);
                        SetType(3);
                    }
                    else if (twoExportValue is Vector4) 
                    {
                        Value = Vector4.Add(new Vector4((float)oneExportValue), (Vector4)twoExportValue);
                        SetType(4);
                    }
                    else if (twoExportValue is Color) 
                    {
                        Value = Vector4.Add(new Vector4((float)oneExportValue), (Vector4)twoExportValue);
                        SetType(4);
                    }
                    else
                    {
                        return;
                    }
                }
                else if (oneExportValue is Vector2)
                {
                    SetType(2);
                    if (twoExportValue is double)
                    {
                        Value = Vector2.Add((Vector2)oneExportValue,new Vector2( (float)twoExportValue));
                   
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector2.Add((Vector2)oneExportValue, (Vector2)twoExportValue);

                    }
                    else if (twoExportValue is Vector3)
                    {
                        var v3 = (Vector2)twoExportValue;
                        Value = Vector2.Add((Vector2)oneExportValue,new Vector2(v3.X,v3.Y));
                       
                    }
                    else if (twoExportValue is Vector4)
                    {
                        var v4 = (Vector4)twoExportValue;
                        Value =Vector2.Add((Vector2)oneExportValue,new Vector2(v4.X,v4.Y));
                    }
                    else
                    {
                        return;
                    }
                }
                else if (oneExportValue is Vector3)
                {
                    SetType(3);
                    if (twoExportValue is double)
                    {
                        Value = Vector3.Add((Vector3)oneExportValue,new Vector3( (float)twoExportValue));
 
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector3.Add((Vector3)oneExportValue,new Vector3((Vector2)twoExportValue,1));
                 

                    }
                    else if (twoExportValue is Vector3)
                    {
                        Value = Vector3.Add((Vector3)oneExportValue, (Vector3)twoExportValue);
                     
                    }
                    else if (twoExportValue is Vector4)
                    {
                        var v4 = (Vector4)twoExportValue;
                        Value = Vector3.Add((Vector3)oneExportValue,new Vector3(v4.X,v4.Y,v4.Z));
                      

                    }
                    else
                    {
                        return;
                    }
                }
                else if (oneExportValue is Vector4)
                {
                    SetType(4);
                    if (twoExportValue is double)
                    {
                        Value = Vector4.Add((Vector4)oneExportValue,new Vector4( (float)twoExportValue));
                        Type = OutputType.Vector4;
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector4.Add((Vector4)oneExportValue, new Vector4((Vector2)twoExportValue,1,1));
                        Type = OutputType.Vector4;

                    }
                    else if (twoExportValue is Vector3)
                    {
                        Value = Vector4.Add((Vector4)oneExportValue,new Vector4( (Vector3)twoExportValue,1));
                        Type = OutputType.Vector4;
                    }
                    else if (twoExportValue is Vector4)
                    {
                       
                        var v4 = (Vector4)twoExportValue;
                        Value = Vector4.Add((Vector4)oneExportValue, v4);
                      
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }

                
               
            }
            catch
            {
                twoConnectElement.Exporter.Disconnect(twoConnectElement);
            }
        }


       

        void GetOneExportValue()
        {
            if (oneConnectElement != null)
            {
                try
                {
                    oneExportValue = GetProvideValue(oneConnectElement.Exporter, oneExportValueName);
                }
                catch
                {
                    oneConnectElement.Exporter.Disconnect(oneConnectElement);
                }
            }
        }

        private void GetTwoExportValue()
        {
            if (twoConnectElement != null)
            {
                try
                {
                    twoExportValue = GetProvideValue(twoConnectElement.Exporter, twoExportValueName);
                }
                catch
                {
                    twoConnectElement.Exporter.Disconnect(twoConnectElement);
                }
            }
        }


        protected override void OnOneProviderDrop(object sender)
        {
            var outputData = EffectOutputControl.outputData;
            //判断是否形成了闭环了
            if (IsChartLoop(outputData.Exporter))
                return;
            if (outputData != null)
            {
                if (outputData.ExporterInfo is string str)
                {
                    oneExportValueName = str;
                }
                else
                {
                    return;
                }
                if (twoConnectElement != null)
                {
                    try
                    {
                        oneExportValue = GetProvideValue(outputData.Exporter, oneExportValueName);


                        if (oneExportValue is double)
                        {
                            if (twoExportValue is double) { }
                            else if (twoExportValue is Vector2) { }
                            else if (twoExportValue is Vector3) { }
                            else if (twoExportValue is Vector4) { }
                            else if (twoExportValue is Color) { }
                            else
                            {
                                return;
                            }
                        }
                        else if (oneExportValue is Vector2)
                        {
                            if (twoExportValue is double) { }
                            else if (twoExportValue is Vector2) { }
                            else if (twoExportValue is Vector3) { }
                            else if (twoExportValue is Vector4) { }
                            else if (twoExportValue is Color) { }
                            else
                            {
                                return;
                            }
                        }
                        else if (oneExportValue is Vector3)
                        {
                            if (twoExportValue is double) { }
                            else if (twoExportValue is Vector2) { }
                            else if (twoExportValue is Vector3) { }
                            else if (twoExportValue is Vector4) { }
                            else if (twoExportValue is Color) { }
                            else
                            {
                                return;
                            }
                        }
                        else if (oneExportValue is Vector4)
                        {
                            if (twoExportValue is double) { }
                            else if (twoExportValue is Vector2) { }
                            else if (twoExportValue is Vector3) { }
                            else if (twoExportValue is Vector4) { }
                            else if (twoExportValue is Color) { }
                            else
                            {
                                return;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                    catch
                    {
                        return;
                    }
                }
                oneExportType = outputData.Exporter.Type;
                CreateConnect( ref oneConnectElement, (ProviderDropControl)sender);
                oneExportValue = GetProvideValue(outputData.Exporter, oneExportValueName);

            }
        }

        protected override void OnTwoProviderDrop(object sender)
        {
            var outputData = EffectOutputControl.outputData;
            //判断是否形成了闭环了
            if (IsChartLoop(outputData.Exporter))
                return;
            if (outputData != null)
            {

                if (outputData.ExporterInfo is string str)
                {
                    twoExportValueName = str;
                }
                else
                {
                    return;
                }

                if (oneConnectElement != null)
                {
                    try
                    {
                        twoExportValue = GetProvideValue(outputData.Exporter, twoExportValueName);

                        if (oneExportValue is double)
                        {
                            if (twoExportValue is double)
                            {
                            }
                            else if (twoExportValue is Vector2) {
                            }
                            else if (twoExportValue is Vector3) {
                            }
                            else if (twoExportValue is Vector4) {
                            }
                            else if (twoExportValue is Color) { }
                            else
                            {
                                return;
                            }
                        }
                        else if (oneExportValue is Vector2)
                        {
                            if (twoExportValue is double) {
                            }
                            else if (twoExportValue is Vector2) {
                            }
                            else if (twoExportValue is Vector3) {
                            }
                            else if (twoExportValue is Vector4) { }
                            else if (twoExportValue is Color) { }
                            else
                            {
                                return;
                            }
                        }
                        else if (oneExportValue is Vector3)
                        {
                            if (twoExportValue is double) { }
                            else if (twoExportValue is Vector2) { }
                            else if (twoExportValue is Vector3) { }
                            else if (twoExportValue is Vector4) { }
                            else if (twoExportValue is Color) { }
                            else
                            {
                                return;
                            }
                        }
                        else if (oneExportValue is Vector4)
                        {
                            if (twoExportValue is double) { }
                            else if (twoExportValue is Vector2) { }
                            else if (twoExportValue is Vector3) { }
                            else if (twoExportValue is Vector4) { }
                            else if (twoExportValue is Color) { }
                            else
                            {
                                return;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                    catch
                    {
                        return;
                    }
                }

                twoExportType = outputData.Exporter.Type;
                CreateConnect(ref twoConnectElement, (ProviderDropControl)sender);
                twoExportValue = GetProvideValue(outputData.Exporter, twoExportValueName);

            }
        }

        protected override void OnProviderDragEnter(object sender, DragEventArgs e)
        {
            var outputData = EffectOutputControl.outputData;
            if (outputData != null)
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
                e.DragUIOverride.Caption = "拖进来了";
            }
        }
    }
}
